
=begin
class Tree < MySingleton

  def nodes
    @nodes = NodeThing.all
  end

  def destination(e)
    path_destination(e) || root_destination
  end

  def path_destination e
    dest = nil
    tail = e.env[:unprocessed_tail]
    tail.reverse.each_with_index { |string_id, i|
      if node = nodes.select {|n| n.string_id == string_id }.first
        (i+1).times {
          e.env[:unprocessed_tail].pop
        }
        dest = node
        break
      end
    }
    dest
  end

  def root_destination
    nodes.select {|n| n.is_root }.first
  end


  class Test < AppTest
    def setup
      DataMapper.setup(:test, :adapter => 'in_memory')
    end

    def node options
      n = NodeThing.new(options)
      n.save
      n
    end

    def root
      node :string_id => 'root', :is_root => true
    end

    def destination url
      Tree.new.destination(Event.new(nil, :env => {
             :unprocessed_tail => url.split('/')
          }))
    end

    def test_empty_path_gives_root_destination
      repository(:test) {
        root = root()
        eq destination(''), root
      }
    end

    def test_path_with_single_node_gives_destination_node
      repository(:test) {
        node(:string_id => 'subpage', :parent_node => root())
        eq destination('subpage').string_id, 'subpage'
      }
    end

    def test_destination_for_sub_sub_page
      repository(:test) {
        node(
          :string_id => 'subsubpage',
          :parent_node => node(
            :string_id => 'subpage', 
            :parent_node => root()
          )
        )
        eq destination('subpage/subsubpage').string_id,
          'subsubpage'
      }
    end
  end
end
=end



class HomePage < Controller

  def index e
    #ResponseCaching.instance.cache(e) {
      # cache is disabled for now
      (Tree.instance.destination(e) || setup_first).respond e
    #}
  end

  def setup_first
    @no_dest ||= Class.new {
      def respond e
        "The webpage needs to be setup through
         <a href=\"admin/\">admin area</a> first."
      end
    }.new
  end


  def css e
    id = e.env[:unprocessed_tail].shift
    CssFile.first(:identity => id).render(e)
  end

end



class KeepWarm < Controller
  plug 'keep_warm'

  def index e
    'Thanks for keeping me warm!'
  end
end
